package org.sparkle.html.api;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.sparkle.html.impl.js.State;
import org.sparkle.html.impl.js.state.HtmlState;

public class AppState implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -5813443326453000796L;
	//private HttpServletRequest request;
	//private HttpServletResponse response;
	private HtmlElement htmlElement;
	private HtmlState htmlState;
	private List<JScript> scriptElement;
	private boolean debug;
	private String path;

	public AppState() {
		//this(null, null, null,null);
	}

	/*
	public AppState(HttpServletRequest request, HttpServletResponse response) {
		this(request,response,null,null);
	}
	*/
	
	/*
	public AppState(HttpServletRequest request, HttpServletResponse response,HtmlElement htmlElement,HtmlState htmlState) {
		super();
		this.request = request;
		this.response = response;
		this.setHtmlElement(htmlElement);
		this.setHtmlState(htmlState);
	}
	*/

	/*
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public HttpServletResponse getResponse() {
		return response;
	}
	*/

	public void setHtmlElement(HtmlElement htmlElement) {
		this.htmlElement = htmlElement;
	}

	public HtmlElement getHtmlElement() {
		if(htmlElement==null)
			htmlElement=new HtmlElement.EmptyElement();
		return htmlElement;
	}

	public HtmlState getHtmlState() {
		if(htmlState==null)
			htmlState=new HtmlState();
		return htmlState;
	}

	public void setHtmlState(HtmlState htmlState) {
		this.htmlState = htmlState;
	}

	public List<JScript> getScriptElement() {
		if(scriptElement==null)
			scriptElement=new ArrayList<JScript>();
		return scriptElement;
	}

	public void setScriptElement(List<JScript> scriptElement) {
		this.scriptElement = scriptElement;
	}
	
	public void addScript(JScript jScript){
		int i=0;
		for(i=0;i<getScriptElement().size(); i++){
			if(getScriptElement().get(i).getInternalId().equalsIgnoreCase(jScript.getInternalId())){
				getScriptElement().remove(i);
				//getScriptElement().add(i,jScript);
				//return;
				break;
			}
		}
		//for render on doGet
		getScriptElement().add(i,jScript);
		//for render on doPost
		getHtmlState().addState("jqscript", new State(jScript.getInternalId(), jScript.getScript()));
	}

	public void setDebug(boolean debug) {
		this.debug=debug;
	}
	
	public boolean isDebug(){
		return debug;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}
	
	/*
	public Application getApplication(){
		return (Application) request.getSession().getAttribute("JQ-APP");
	}
	*/

}
